#!/usr/bin/env python
# -*- coding:utf-8 -*-

import yaml
import os
from configs import globalVariables
'''
【功能】：从Yaml文件读取数据
【入参】：文件名(包含路径)
【返回值】：文件内容(dict类型)
'''

class ReadYaml(object):

    def readYaml(self, yamlPath):
        try:
            with open(yamlPath, 'r', encoding='utf-8') as f:
                content = f.read() #读取到的内容为str格式
                contents = yaml.load(content, Loader=yaml.FullLoader) #将读取内容装换为dict格式
        except Exception as e:
            globalVariables.logger.error("读取Yaml文件出错了~ %s" % e)
        return contents

if __name__ == "__main__":
    # 获取配置文件路径
    curPath = os.path.abspath(os.path.dirname(__file__))
    yamlPath = os.path.abspath(os.path.dirname(curPath) + os.path.sep + "configs/api_trainingAndExam.yml")
    ry = ReadYaml()
    readContent = ry.readYaml(yamlPath)
    print(readContent)
    print(type(readContent))
'''
os.path.dirname(__file__)： 获取文件所在目录的完整路径；
os.path.abspath(path): 返回绝对路径
'''